package com.zw.yzk.kabu.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ExifInterface;

import com.zw.yzk.kabu.data.utils.FileUtils;
import com.zw.yzk.kabu.data.utils.LogUtils;

import java.io.File;
import java.io.IOException;

import id.zelory.compressor.Compressor;

/**
 * Created by zhanwei on 2017/2/12.
 */

public class ImageUtils {

    /**
     * 压缩图片使用默认配置
     *
     * @param context 上下文
     * @param image   图片
     * @return 压缩后图片路径
     */
    public static String compressDefault(Context context, File image) {
        File compressedImageFile = new Compressor.Builder(context)
                .setQuality(90)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .setDestinationDirectoryPath(FileUtils.getImageCompressDir(context))
                .build()
                .compressToFile(image);

        return compressedImageFile.getAbsolutePath();
    }

    /**
     * 压缩图片，根据原图大小设置最合适的压缩大小
     *
     * @param context 上下文
     * @param file    原图
     * @return 压缩后图片
     */
    public static String compress(Context context, File file) {
        if (file.length() < 1024 * 150) {
            //200K以下不做压缩
            return file.getAbsolutePath();
        }

        String filePath = file.getAbsolutePath();
        int width = getImageSize(filePath)[0];
        int height = getImageSize(filePath)[1];
        int thumbW = width % 2 == 1 ? width + 1 : width;
        int thumbH = height % 2 == 1 ? height + 1 : height;

        width = thumbW > thumbH ? thumbH : thumbW;
        height = thumbW > thumbH ? thumbW : thumbH;

        double scale = ((double) width / height);

        if (scale <= 1 && scale > 0.5625) {
            if (height < 1664) {
                if (file.length() / 1024 < 150) return file.getAbsolutePath();
            } else if (height >= 1664 && height < 4990) {
                thumbW = width / 2;
                thumbH = height / 2;
            } else if (height >= 4990 && height < 10240) {
                thumbW = width / 4;
                thumbH = height / 4;
            } else {
                int multiple = height / 1280 == 0 ? 1 : height / 1280;
                thumbW = width / multiple;
                thumbH = height / multiple;
            }
        } else if (scale <= 0.5625 && scale > 0.5) {
            if (height < 1280 && file.length() / 1024 < 200) return file.getAbsolutePath();

            int multiple = height / 1280 == 0 ? 1 : height / 1280;
            thumbW = width / multiple;
            thumbH = height / multiple;
        } else {
            int multiple = (int) Math.ceil(height / (1280.0 / scale));
            thumbW = width / multiple;
            thumbH = height / multiple;
        }

        File compressedImageFile = new Compressor.Builder(context)
                .setQuality(75)
                .setMaxHeight(thumbH)
                .setMaxWidth(thumbW)
                .setCompressFormat(Bitmap.CompressFormat.JPEG)
                .setDestinationDirectoryPath(FileUtils.getImageCompressDir(context))
                .build()
                .compressToFile(file);

        return compressedImageFile.getAbsolutePath();
    }

    /**
     * obtain the image's width and height
     *
     * @param imagePath the path of image
     */
    private static int[] getImageSize(String imagePath) {
        int[] res = new int[2];

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inSampleSize = 1;
        BitmapFactory.decodeFile(imagePath, options);

        res[0] = options.outWidth;
        res[1] = options.outHeight;

        return res;
    }

    /**
     * 获取图片旋转的角度
     * @param filepath
     * @return
     */
    public static int getExifOrientation(String filepath) {
        int degree = 0;
        ExifInterface exif = null;
        try {
            exif = new ExifInterface(filepath);
        } catch (IOException ex) {
            LogUtils.d("zhanwei", "cannot read exif" + ex);
        }
        if (exif != null) {
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch(orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        degree = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        degree = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        degree = 270;
                        break;
                }
            }
        }
        return degree;
    }
}
